{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-24T13:58:29.178501Z",
     "start_time": "2025-04-24T13:58:29.162792Z"
    }
   },
   "outputs": [],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Bar, Line, Pie, WordCloud\n",
    "from pyecharts.globals import SymbolType\n",
    "from pyecharts.commons.utils import JsCode\n",
    "from pyecharts.charts import Page\n",
    "import pandas as pd\n",
    "from collections import Counter\n",
    "import jieba\n",
    "from snownlp import SnowNLP\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            昵称 性别  地区  点赞数                                                评论\n",
      "0        si陈梓诺  男  河南  704           这一下外国网友要给中国人加一个吃槟榔的标签了前几天积累的正面影响一下子大打折扣\n",
      "1      英雄联盟神吐槽  男  江西    7                     英雄联盟神吐槽再不进群就没位置了快进来一起玩新群吐槽开黑群\n",
      "2        游戏圈瓜王  男  江西  591                                送槟榔是什么心理况且这还是个岁的孩子\n",
      "3          乙游君  男  江西  346                      这玩意会上瘾吧跟毒品差不多最后口腔都纤维化了脸嚼变形了衰\n",
      "4      小星星沉迷柠檬  男  广东  178                                        评论里的湖南人真恐怖\n",
      "..         ... ..  ..  ...                                               ...\n",
      "995        编辑态  男  四川    0                                      槟榔管理什么时候和烟持平\n",
      "996     zeng-城  男  广东    2                               湖南人正常吃瓜毕竟高速都是出了名的地方\n",
      "997    铁骨铮铮常凯申  男  河南    1                                这和咱们人在美国被投喂大麻一个道理吧\n",
      "998  指间有个太阳666  男  辽宁    1                                        别太把外国人当回事了\n",
      "999     ask314  男  山东    1  无所谓吧美国那边明星时不时还搞口大麻虽然是致癌物但离毒品等级差得远呢来口也算特色上升不到吃相难看\n",
      "\n",
      "[1000 rows x 5 columns]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import re\n",
    "\n",
    "# 读取CSV文件\n",
    "df = pd.read_csv('微博评论.csv')\n",
    "\n",
    "# 替换性别列中的值\n",
    "df['性别'] = df['性别'].replace({'f': '女', 'm': '男'})\n",
    "\n",
    "# 删除地区列中的“来自”字样\n",
    "df['地区'] = df['地区'].str.replace('来自', '')\n",
    "\n",
    "# 定义清洗函数，仅保留中文\n",
    "def clean_comment(text):\n",
    "    # 保留中文字符，去除其他字符\n",
    "    return ''.join(re.findall(r'[\\u4e00-\\u9fa5]+', str(text)))\n",
    "\n",
    "# 清洗评论列\n",
    "df['评论'] = df['评论'].apply(clean_comment)\n",
    "\n",
    "# 保存处理后的数据到新CSV文件\n",
    "df.to_csv('cleaned_微博评论.csv', index=False, encoding='utf-8-sig')\n",
    "\n",
    "# 打印处理后的数据\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 读取数据\n",
    "try:\n",
    "    df = pd.read_csv('cleaned_微博评论.csv', encoding='utf-8')  # 明确指定编码\n",
    "except FileNotFoundError:\n",
    "    raise FileNotFoundError(\"CSV文件未找到，请检查文件路径\")\n",
    "except Exception as e:\n",
    "    raise Exception(f\"读取文件时出错: {str(e)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-24T13:58:29.209194Z",
     "start_time": "2025-04-24T13:58:29.179685Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/v5/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"b6d8f313ddcf45e09f7c0ef82efabada\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_b6d8f313ddcf45e09f7c0ef82efabada = echarts.init(\n",
       "                    document.getElementById('b6d8f313ddcf45e09f7c0ef82efabada'), 'white', {renderer: 'canvas'});\n",
       "                var option_b6d8f313ddcf45e09f7c0ef82efabada = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"aria\": {\n",
       "        \"enabled\": false\n",
       "    },\n",
       "    \"color\": [\n",
       "        \"#5470c6\",\n",
       "        \"#91cc75\",\n",
       "        \"#fac858\",\n",
       "        \"#ee6666\",\n",
       "        \"#73c0de\",\n",
       "        \"#3ba272\",\n",
       "        \"#fc8452\",\n",
       "        \"#9a60b4\",\n",
       "        \"#ea7ccc\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"line\",\n",
       "            \"name\": \"\\u6570\\u91cf\",\n",
       "            \"connectNulls\": false,\n",
       "            \"xAxisIndex\": 0,\n",
       "            \"symbol\": \"triangle\",\n",
       "            \"symbolSize\": 10,\n",
       "            \"showSymbol\": true,\n",
       "            \"smooth\": false,\n",
       "            \"clip\": true,\n",
       "            \"step\": false,\n",
       "            \"stackStrategy\": \"samesign\",\n",
       "            \"data\": [\n",
       "                [\n",
       "                    \"\\u5e7f\\u4e1c\",\n",
       "                    134\n",
       "                ],\n",
       "                [\n",
       "                    \"\\u6e56\\u5357\",\n",
       "                    92\n",
       "                ],\n",
       "                [\n",
       "                    \"\\u6c5f\\u82cf\",\n",
       "                    82\n",
       "                ],\n",
       "                [\n",
       "                    \"\\u5317\\u4eac\",\n",
       "                    68\n",
       "                ],\n",
       "                [\n",
       "                    \"\\u6d59\\u6c5f\",\n",
       "                    65\n",
       "                ],\n",
       "                [\n",
       "                    \"\\u56db\\u5ddd\",\n",
       "                    53\n",
       "                ],\n",
       "                [\n",
       "                    \"\\u5c71\\u4e1c\",\n",
       "                    48\n",
       "                ],\n",
       "                [\n",
       "                    \"\\u4e0a\\u6d77\",\n",
       "                    48\n",
       "                ],\n",
       "                [\n",
       "                    \"\\u6e56\\u5317\",\n",
       "                    44\n",
       "                ],\n",
       "                [\n",
       "                    \"\\u798f\\u5efa\",\n",
       "                    38\n",
       "                ]\n",
       "            ],\n",
       "            \"hoverAnimation\": true,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"margin\": 8,\n",
       "                \"valueAnimation\": false\n",
       "            },\n",
       "            \"logBase\": 10,\n",
       "            \"seriesLayoutBy\": \"column\",\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 3,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"dashed\"\n",
       "            },\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            },\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 0\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u6570\\u91cf\"\n",
       "            ],\n",
       "            \"selected\": {},\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14,\n",
       "            \"backgroundColor\": \"transparent\",\n",
       "            \"borderColor\": \"#ccc\",\n",
       "            \"borderRadius\": 0,\n",
       "            \"pageButtonItemGap\": 5,\n",
       "            \"pageButtonPosition\": \"end\",\n",
       "            \"pageFormatter\": \"{current}/{total}\",\n",
       "            \"pageIconColor\": \"#2f4554\",\n",
       "            \"pageIconInactiveColor\": \"#aaa\",\n",
       "            \"pageIconSize\": 15,\n",
       "            \"animationDurationUpdate\": 800,\n",
       "            \"selector\": false,\n",
       "            \"selectorPosition\": \"auto\",\n",
       "            \"selectorItemGap\": 7,\n",
       "            \"selectorButtonGap\": 10\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"enterable\": false,\n",
       "        \"confine\": false,\n",
       "        \"appendToBody\": false,\n",
       "        \"transitionDuration\": 0.4,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5,\n",
       "        \"order\": \"seriesAsc\"\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"name\": \"\\u5730\\u533a\",\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"axisLabel\": {\n",
       "                \"show\": true,\n",
       "                \"rotate\": 30,\n",
       "                \"margin\": 8,\n",
       "                \"valueAnimation\": false\n",
       "            },\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": true,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"animation\": true,\n",
       "            \"animationThreshold\": 2000,\n",
       "            \"animationDuration\": 1000,\n",
       "            \"animationEasing\": \"cubicOut\",\n",
       "            \"animationDelay\": 0,\n",
       "            \"animationDurationUpdate\": 300,\n",
       "            \"animationEasingUpdate\": \"cubicOut\",\n",
       "            \"animationDelayUpdate\": 0,\n",
       "            \"data\": [\n",
       "                \"\\u5e7f\\u4e1c\",\n",
       "                \"\\u6e56\\u5357\",\n",
       "                \"\\u6c5f\\u82cf\",\n",
       "                \"\\u5317\\u4eac\",\n",
       "                \"\\u6d59\\u6c5f\",\n",
       "                \"\\u56db\\u5ddd\",\n",
       "                \"\\u5c71\\u4e1c\",\n",
       "                \"\\u4e0a\\u6d77\",\n",
       "                \"\\u6e56\\u5317\",\n",
       "                \"\\u798f\\u5efa\"\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"name\": \"\\u6570\\u91cf\",\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": true,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"animation\": true,\n",
       "            \"animationThreshold\": 2000,\n",
       "            \"animationDuration\": 1000,\n",
       "            \"animationEasing\": \"cubicOut\",\n",
       "            \"animationDelay\": 0,\n",
       "            \"animationDurationUpdate\": 300,\n",
       "            \"animationEasingUpdate\": \"cubicOut\",\n",
       "            \"animationDelayUpdate\": 0\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"text\": \"\\u5730\\u533a\\u5206\\u5e03TOP10\",\n",
       "            \"target\": \"blank\",\n",
       "            \"subtarget\": \"blank\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"textAlign\": \"auto\",\n",
       "            \"textVerticalAlign\": \"auto\",\n",
       "            \"triggerEvent\": false\n",
       "        }\n",
       "    ],\n",
       "    \"dataZoom\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"type\": \"slider\",\n",
       "            \"showDetail\": true,\n",
       "            \"showDataShadow\": true,\n",
       "            \"realtime\": true,\n",
       "            \"start\": 20,\n",
       "            \"end\": 80,\n",
       "            \"orient\": \"horizontal\",\n",
       "            \"zoomLock\": false,\n",
       "            \"filterMode\": \"filter\"\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_b6d8f313ddcf45e09f7c0ef82efabada.setOption(option_b6d8f313ddcf45e09f7c0ef82efabada);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x1b9da6f8490>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# 2. 地区折线图 (添加列存在性检查)\n",
    "if '地区' not in df.columns:\n",
    "    raise ValueError(\"CSV文件中缺少'地区'列\")\n",
    "\n",
    "region_counts = df['地区'].value_counts().head(10)  # 只显示前10个地区\n",
    "\n",
    "region_line = (\n",
    "    Line()\n",
    "    .add_xaxis(region_counts.index.tolist())\n",
    "    .add_yaxis(\"数量\", region_counts.values.tolist(),\n",
    "               symbol=\"triangle\",\n",
    "               symbol_size=10,\n",
    "               linestyle_opts=opts.LineStyleOpts(width=3, type_=\"dashed\"))\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title=\"地区分布TOP10\"),\n",
    "        xaxis_opts=opts.AxisOpts(name=\"地区\", axislabel_opts=opts.LabelOpts(rotate=30)),\n",
    "        yaxis_opts=opts.AxisOpts(name=\"数量\"),\n",
    "        datazoom_opts=[opts.DataZoomOpts()]\n",
    "    )\n",
    ")\n",
    "region_line.render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-24T13:58:29.225032Z",
     "start_time": "2025-04-24T13:58:29.210195Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/v5/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"1f61114fd4f84953aa9648450f7430f6\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_1f61114fd4f84953aa9648450f7430f6 = echarts.init(\n",
       "                    document.getElementById('1f61114fd4f84953aa9648450f7430f6'), 'white', {renderer: 'canvas'});\n",
       "                var option_1f61114fd4f84953aa9648450f7430f6 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"aria\": {\n",
       "        \"enabled\": false\n",
       "    },\n",
       "    \"color\": [\n",
       "        \"#5470c6\",\n",
       "        \"#91cc75\",\n",
       "        \"#fac858\",\n",
       "        \"#ee6666\",\n",
       "        \"#73c0de\",\n",
       "        \"#3ba272\",\n",
       "        \"#fc8452\",\n",
       "        \"#9a60b4\",\n",
       "        \"#ea7ccc\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"pie\",\n",
       "            \"colorBy\": \"data\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"selectedMode\": false,\n",
       "            \"selectedOffset\": 10,\n",
       "            \"clockwise\": true,\n",
       "            \"startAngle\": 90,\n",
       "            \"minAngle\": 0,\n",
       "            \"minShowLabelAngle\": 0,\n",
       "            \"avoidLabelOverlap\": true,\n",
       "            \"stillShowZeroSum\": true,\n",
       "            \"percentPrecision\": 2,\n",
       "            \"showEmptyCircle\": true,\n",
       "            \"emptyCircleStyle\": {\n",
       "                \"color\": \"lightgray\",\n",
       "                \"borderColor\": \"#000\",\n",
       "                \"borderWidth\": 0,\n",
       "                \"borderType\": \"solid\",\n",
       "                \"borderDashOffset\": 0,\n",
       "                \"borderCap\": \"butt\",\n",
       "                \"borderJoin\": \"bevel\",\n",
       "                \"borderMiterLimit\": 10,\n",
       "                \"opacity\": 1\n",
       "            },\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"\\u7537\",\n",
       "                    \"value\": 624\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5973\",\n",
       "                    \"value\": 376\n",
       "                }\n",
       "            ],\n",
       "            \"radius\": [\n",
       "                \"30%\",\n",
       "                \"75%\"\n",
       "            ],\n",
       "            \"center\": [\n",
       "                \"25%\",\n",
       "                \"50%\"\n",
       "            ],\n",
       "            \"roseType\": \"radius\",\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": \"{b}: {c} ({d}%)\",\n",
       "                \"valueAnimation\": false\n",
       "            },\n",
       "            \"labelLine\": {\n",
       "                \"show\": true,\n",
       "                \"showAbove\": false,\n",
       "                \"length\": 15,\n",
       "                \"length2\": 15,\n",
       "                \"smooth\": false,\n",
       "                \"minTurnAngle\": 90,\n",
       "                \"maxSurfaceAngle\": 90\n",
       "            },\n",
       "            \"tooltip\": {\n",
       "                \"show\": true,\n",
       "                \"trigger\": \"item\",\n",
       "                \"triggerOn\": \"mousemove|click\",\n",
       "                \"axisPointer\": {\n",
       "                    \"type\": \"line\"\n",
       "                },\n",
       "                \"showContent\": true,\n",
       "                \"alwaysShowContent\": false,\n",
       "                \"showDelay\": 0,\n",
       "                \"hideDelay\": 100,\n",
       "                \"enterable\": false,\n",
       "                \"confine\": false,\n",
       "                \"appendToBody\": false,\n",
       "                \"transitionDuration\": 0.4,\n",
       "                \"formatter\": \"{a} <br/>{b}: {c} ({d}%)\",\n",
       "                \"textStyle\": {\n",
       "                    \"fontSize\": 14\n",
       "                },\n",
       "                \"borderWidth\": 0,\n",
       "                \"padding\": 5,\n",
       "                \"order\": \"seriesAsc\"\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u7537\",\n",
       "                \"\\u5973\"\n",
       "            ],\n",
       "            \"selected\": {},\n",
       "            \"show\": true,\n",
       "            \"left\": \"15%\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14,\n",
       "            \"backgroundColor\": \"transparent\",\n",
       "            \"borderColor\": \"#ccc\",\n",
       "            \"borderRadius\": 0,\n",
       "            \"pageButtonItemGap\": 5,\n",
       "            \"pageButtonPosition\": \"end\",\n",
       "            \"pageFormatter\": \"{current}/{total}\",\n",
       "            \"pageIconColor\": \"#2f4554\",\n",
       "            \"pageIconInactiveColor\": \"#aaa\",\n",
       "            \"pageIconSize\": 15,\n",
       "            \"animationDurationUpdate\": 800,\n",
       "            \"selector\": false,\n",
       "            \"selectorPosition\": \"auto\",\n",
       "            \"selectorItemGap\": 7,\n",
       "            \"selectorButtonGap\": 10\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"enterable\": false,\n",
       "        \"confine\": false,\n",
       "        \"appendToBody\": false,\n",
       "        \"transitionDuration\": 0.4,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5,\n",
       "        \"order\": \"seriesAsc\"\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"text\": \"\\u6027\\u522b\\u5206\\u5e03\",\n",
       "            \"target\": \"blank\",\n",
       "            \"subtarget\": \"blank\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"textAlign\": \"auto\",\n",
       "            \"textVerticalAlign\": \"auto\",\n",
       "            \"triggerEvent\": false\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_1f61114fd4f84953aa9648450f7430f6.setOption(option_1f61114fd4f84953aa9648450f7430f6);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x1b9da6e33a0>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# 3. 性别饼图 (添加列存在性检查)\n",
    "if '性别' not in df.columns:\n",
    "    raise ValueError(\"CSV文件中缺少'性别'列\")\n",
    "\n",
    "gender_counts = df['性别'].value_counts()\n",
    "\n",
    "gender_pie = (\n",
    "    Pie()\n",
    "    .add(\n",
    "        \"\",\n",
    "        [list(z) for z in zip(gender_counts.index.tolist(), gender_counts.values.tolist())],\n",
    "        radius=[\"30%\", \"75%\"],\n",
    "        center=[\"25%\", \"50%\"],\n",
    "        rosetype=\"radius\"\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title=\"性别分布\"),\n",
    "        legend_opts=opts.LegendOpts(pos_left=\"15%\"),\n",
    "    )\n",
    "    .set_series_opts(\n",
    "        label_opts=opts.LabelOpts(formatter=\"{b}: {c} ({d}%)\"),\n",
    "        tooltip_opts=opts.TooltipOpts(\n",
    "            trigger=\"item\", formatter=\"{a} <br/>{b}: {c} ({d}%)\")\n",
    "    )\n",
    ")\n",
    "gender_pie.render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-24T13:58:29.320635Z",
     "start_time": "2025-04-24T13:58:29.226413Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Loading model from cache C:\\Users\\wangx\\AppData\\Local\\Temp\\jieba.cache\n",
      "Loading model cost 0.589 seconds.\n",
      "Prefix dict has been built successfully.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/v5/echarts.min', 'echarts-wordcloud':'https://assets.pyecharts.org/assets/v5/echarts-wordcloud.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"2be96b6d89c04ed19059e3ab4607278a\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts', 'echarts-wordcloud'], function(echarts) {\n",
       "                var chart_2be96b6d89c04ed19059e3ab4607278a = echarts.init(\n",
       "                    document.getElementById('2be96b6d89c04ed19059e3ab4607278a'), 'white', {renderer: 'canvas'});\n",
       "                var option_2be96b6d89c04ed19059e3ab4607278a = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"aria\": {\n",
       "        \"enabled\": false\n",
       "    },\n",
       "    \"color\": [\n",
       "        \"#5470c6\",\n",
       "        \"#91cc75\",\n",
       "        \"#fac858\",\n",
       "        \"#ee6666\",\n",
       "        \"#73c0de\",\n",
       "        \"#3ba272\",\n",
       "        \"#fc8452\",\n",
       "        \"#9a60b4\",\n",
       "        \"#ea7ccc\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"wordCloud\",\n",
       "            \"shape\": \"diamond\",\n",
       "            \"rotationRange\": [\n",
       "                0,\n",
       "                0\n",
       "            ],\n",
       "            \"rotationStep\": 45,\n",
       "            \"girdSize\": 20,\n",
       "            \"sizeRange\": [\n",
       "                20,\n",
       "                100\n",
       "            ],\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"\\u8fd9\\u4e00\\u4e0b\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(21,99,8)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5916\\u56fd\",\n",
       "                    \"value\": 6,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(121,52,88)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7f51\\u53cb\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(65,83,65)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e2d\\u56fd\",\n",
       "                    \"value\": 63,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(6,82,107)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4eba\\u52a0\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(123,83,146)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e00\\u4e2a\",\n",
       "                    \"value\": 39,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(146,136,96)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u69df\\u6994\",\n",
       "                    \"value\": 172,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(120,134,62)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6807\\u7b7e\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(77,133,111)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u51e0\\u5929\",\n",
       "                    \"value\": 2,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(15,64,119)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u79ef\\u7d2f\",\n",
       "                    \"value\": 3,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(77,95,8)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6b63\\u9762\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(134,50,72)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5f71\\u54cd\",\n",
       "                    \"value\": 3,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(31,42,39)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e00\\u4e0b\\u5b50\",\n",
       "                    \"value\": 2,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(26,98,38)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5927\\u6253\\u6298\\u6263\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(29,102,50)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u82f1\\u96c4\",\n",
       "                    \"value\": 2,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(34,124,3)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u8054\\u76df\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(110,34,68)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u795e\\u5410\\u69fd\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(49,2,31)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u8fdb\\u7fa4\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(17,123,157)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4f4d\\u7f6e\",\n",
       "                    \"value\": 4,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(100,43,116)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5feb\\u8fdb\\u6765\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(73,144,152)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e00\\u8d77\",\n",
       "                    \"value\": 4,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(63,141,31)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u73a9\\u65b0\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(57,7,58)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7fa4\\u5410\\u69fd\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(154,125,62)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u9ed1\\u7fa4\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(20,87,76)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4ec0\\u4e48\",\n",
       "                    \"value\": 26,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(130,15,142)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5fc3\\u7406\",\n",
       "                    \"value\": 3,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(110,97,38)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u51b5\\u4e14\",\n",
       "                    \"value\": 2,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(14,77,2)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u8fd8\\u662f\",\n",
       "                    \"value\": 12,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(127,145,123)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5b69\\u5b50\",\n",
       "                    \"value\": 5,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(91,40,63)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u73a9\\u610f\",\n",
       "                    \"value\": 27,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(148,142,59)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e0a\\u763e\",\n",
       "                    \"value\": 10,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(98,129,54)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6bd2\\u54c1\",\n",
       "                    \"value\": 26,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(68,89,137)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5dee\\u4e0d\\u591a\",\n",
       "                    \"value\": 3,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(52,74,56)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6700\\u540e\",\n",
       "                    \"value\": 10,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(23,65,96)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u53e3\\u8154\",\n",
       "                    \"value\": 3,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(110,5,142)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7ea4\\u7ef4\\u5316\",\n",
       "                    \"value\": 2,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(91,149,158)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u53d8\\u5f62\",\n",
       "                    \"value\": 2,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(18,53,66)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u8bc4\\u8bba\",\n",
       "                    \"value\": 6,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(58,63,13)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6e56\\u5357\\u4eba\",\n",
       "                    \"value\": 23,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(37,104,159)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6050\\u6016\",\n",
       "                    \"value\": 2,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(35,135,154)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7f8e\\u56fd\",\n",
       "                    \"value\": 22,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(97,35,156)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5b9a\\u4f4d\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(133,51,138)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u8f6f\\u6027\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(81,22,24)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7981\\u8fd0\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(2,16,131)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7981\\u552e\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(112,67,119)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5bf9\\u65b9\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(104,88,46)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e0d\\u77e5\\u60c5\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(108,38,94)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u60c5\\u51b5\",\n",
       "                    \"value\": 3,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(128,157,47)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u63a8\\u8350\",\n",
       "                    \"value\": 4,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(18,47,52)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7b80\\u76f4\",\n",
       "                    \"value\": 3,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(156,59,23)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u755c\\u751f\",\n",
       "                    \"value\": 2,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(28,129,33)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u884c\\u4e3a\",\n",
       "                    \"value\": 4,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(13,64,92)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e22\\u4eba\",\n",
       "                    \"value\": 11,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(79,41,63)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5927\\u53d1\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(52,116,26)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u60f3\\u4f1a\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(24,5,106)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e0d\\u4f1a\",\n",
       "                    \"value\": 14,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(10,109,95)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6709\\u4eba\",\n",
       "                    \"value\": 5,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(104,59,27)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u8fd9\\u4e48\",\n",
       "                    \"value\": 13,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(146,41,63)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u8fd9\\u4e0b\",\n",
       "                    \"value\": 3,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(39,24,20)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4ee5\\u540e\",\n",
       "                    \"value\": 6,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(114,98,29)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u964c\\u751f\\u4eba\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(52,155,117)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e1c\\u897f\",\n",
       "                    \"value\": 39,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(96,68,2)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e0d\\u6562\",\n",
       "                    \"value\": 5,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(25,15,81)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e3b\\u8981\",\n",
       "                    \"value\": 4,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(64,112,54)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6ca1\\u60f3\\u5230\",\n",
       "                    \"value\": 3,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(131,6,61)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e3a\\u5565\",\n",
       "                    \"value\": 7,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(76,35,54)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5728\\u4e4e\",\n",
       "                    \"value\": 5,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(112,63,81)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5916\\u56fd\\u4eba\",\n",
       "                    \"value\": 9,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(6,124,28)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u770b\\u6cd5\",\n",
       "                    \"value\": 4,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(70,146,0)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u8fd9\\u4eba\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(65,160,152)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6545\\u610f\",\n",
       "                    \"value\": 4,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(68,132,127)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7532\\u4ea2\",\n",
       "                    \"value\": 78,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(89,9,123)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u957f\\u6c99\",\n",
       "                    \"value\": 97,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(54,39,157)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u600e\\u4e48\",\n",
       "                    \"value\": 31,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(51,29,40)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e22\\u8138\",\n",
       "                    \"value\": 9,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(81,150,70)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4f60\\u4eec\",\n",
       "                    \"value\": 7,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(65,159,130)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u662f\\u4e0d\\u662f\",\n",
       "                    \"value\": 12,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(60,90,144)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u81ea\\u5351\",\n",
       "                    \"value\": 3,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(16,9,68)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u654f\\u611f\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(85,26,148)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u60b2\\u5141\\u60b2\",\n",
       "                    \"value\": 5,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(155,129,26)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u52a8\\u4e0d\\u52a8\",\n",
       "                    \"value\": 2,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(61,96,104)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u56fd\\u5916\",\n",
       "                    \"value\": 10,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(86,48,122)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e4b\\u524d\",\n",
       "                    \"value\": 8,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(110,140,38)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u90a3\\u4e48\",\n",
       "                    \"value\": 17,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(32,151,62)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u53ef\\u4ee5\",\n",
       "                    \"value\": 15,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(26,78,146)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u76f4\\u63a5\",\n",
       "                    \"value\": 9,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(152,96,42)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u65c1\\u8fb9\",\n",
       "                    \"value\": 2,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(137,24,149)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u591a\\u4eba\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(57,137,51)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6559\\u6559\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(58,4,147)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u77e5\\u9053\",\n",
       "                    \"value\": 27,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(47,137,98)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u8fd9\\u4e2a\",\n",
       "                    \"value\": 46,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(52,82,39)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e0d\\u80fd\",\n",
       "                    \"value\": 10,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(156,37,56)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u88c2\\u5f00\",\n",
       "                    \"value\": 7,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(17,122,158)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5076\\u5c14\",\n",
       "                    \"value\": 5,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(98,11,149)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e00\\u9897\",\n",
       "                    \"value\": 4,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(22,107,1)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6ca1\\u4e8b\",\n",
       "                    \"value\": 6,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(97,135,131)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6ca1\\u5403\\u8fc7\",\n",
       "                    \"value\": 7,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(157,64,3)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5c31\\u662f\",\n",
       "                    \"value\": 34,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(109,145,25)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7eaf\\u574f\",\n",
       "                    \"value\": 1,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(119,38,139)\"\n",
       "                    }\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u76f4\\u64ad\",\n",
       "                    \"value\": 26,\n",
       "                    \"textStyle\": {\n",
       "                        \"color\": \"rgb(21,19,73)\"\n",
       "                    }\n",
       "                }\n",
       "            ],\n",
       "            \"drawOutOfBound\": false,\n",
       "            \"textStyle\": {\n",
       "                \"normal\": {\n",
       "                    \"fontFamily\": \"Microsoft YaHei\"\n",
       "                },\n",
       "                \"emphasis\": {}\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [],\n",
       "            \"selected\": {},\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14,\n",
       "            \"backgroundColor\": \"transparent\",\n",
       "            \"borderColor\": \"#ccc\",\n",
       "            \"borderRadius\": 0,\n",
       "            \"pageButtonItemGap\": 5,\n",
       "            \"pageButtonPosition\": \"end\",\n",
       "            \"pageFormatter\": \"{current}/{total}\",\n",
       "            \"pageIconColor\": \"#2f4554\",\n",
       "            \"pageIconInactiveColor\": \"#aaa\",\n",
       "            \"pageIconSize\": 15,\n",
       "            \"animationDurationUpdate\": 800,\n",
       "            \"selector\": false,\n",
       "            \"selectorPosition\": \"auto\",\n",
       "            \"selectorItemGap\": 7,\n",
       "            \"selectorButtonGap\": 10\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"enterable\": false,\n",
       "        \"confine\": false,\n",
       "        \"appendToBody\": false,\n",
       "        \"transitionDuration\": 0.4,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5,\n",
       "        \"order\": \"seriesAsc\"\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"text\": \"\\u8bc4\\u8bba\\u9ad8\\u9891\\u8bcd\\u4e91\",\n",
       "            \"target\": \"blank\",\n",
       "            \"subtarget\": \"blank\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"textAlign\": \"auto\",\n",
       "            \"textVerticalAlign\": \"auto\",\n",
       "            \"triggerEvent\": false\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_2be96b6d89c04ed19059e3ab4607278a.setOption(option_2be96b6d89c04ed19059e3ab4607278a);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x1b9f9da9e20>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# 4. 评论词云图\n",
    "if '评论' not in df.columns:\n",
    "    raise ValueError(\"CSV文件中缺少'评论'列\")\n",
    "\n",
    "text = \" \".join(df['评论'].astype(str).tolist())\n",
    "words = jieba.lcut(text)\n",
    "word_counts = Counter(words)\n",
    "# 添加常用中文停用词过滤\n",
    "stopwords = set(['的', '了', '和', '是', '我', '你', '在', '这', '有', '就', '不', '人', '都', '也', '说'])\n",
    "filtered_words = [(word, count) for word, count in word_counts.items() \n",
    "                 if len(word) > 1 and not word.isdigit() and word not in stopwords][:100]\n",
    "\n",
    "comment_wordcloud = (\n",
    "    WordCloud()\n",
    "    .add(\n",
    "        \"\", \n",
    "        filtered_words, \n",
    "        word_size_range=[20, 100], \n",
    "        shape=SymbolType.DIAMOND,\n",
    "        textstyle_opts=opts.TextStyleOpts(font_family=\"Microsoft YaHei\")\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title=\"评论高频词云\"),\n",
    "        tooltip_opts=opts.TooltipOpts(is_show=True)\n",
    "    )\n",
    ")\n",
    "comment_wordcloud.render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-24T13:58:32.992798Z",
     "start_time": "2025-04-24T13:58:29.323220Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在执行情感分析，这可能需要一些时间...\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/v5/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"843e121b605b487eb7b4dbae98fbc420\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_843e121b605b487eb7b4dbae98fbc420 = echarts.init(\n",
       "                    document.getElementById('843e121b605b487eb7b4dbae98fbc420'), 'white', {renderer: 'canvas'});\n",
       "                var option_843e121b605b487eb7b4dbae98fbc420 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"aria\": {\n",
       "        \"enabled\": false\n",
       "    },\n",
       "    \"color\": [\n",
       "        \"#5470c6\",\n",
       "        \"#91cc75\",\n",
       "        \"#fac858\",\n",
       "        \"#ee6666\",\n",
       "        \"#73c0de\",\n",
       "        \"#3ba272\",\n",
       "        \"#fc8452\",\n",
       "        \"#9a60b4\",\n",
       "        \"#ea7ccc\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"bar\",\n",
       "            \"name\": \"\\u8bc4\\u8bba\\u6570\\u91cf\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"data\": [\n",
       "                505,\n",
       "                328,\n",
       "                167\n",
       "            ],\n",
       "            \"realtimeSort\": false,\n",
       "            \"showBackground\": false,\n",
       "            \"stackStrategy\": \"samesign\",\n",
       "            \"cursor\": \"pointer\",\n",
       "            \"barMinHeight\": 0,\n",
       "            \"barCategoryGap\": \"20%\",\n",
       "            \"barGap\": \"30%\",\n",
       "            \"large\": false,\n",
       "            \"largeThreshold\": 400,\n",
       "            \"seriesLayoutBy\": \"column\",\n",
       "            \"datasetIndex\": 0,\n",
       "            \"clip\": true,\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 2,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8,\n",
       "                \"valueAnimation\": false\n",
       "            },\n",
       "            \"itemStyle\": {\n",
       "                \"normal\": {\n",
       "                    \"color\": function(params) {                    if (params.value > 150) {                        return '#E15457';                    } else if (params.value > 100) {                        return '#F9A589';                    } else {                        return '#D7DA8B';                    }                }\n",
       "                }\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u8bc4\\u8bba\\u6570\\u91cf\"\n",
       "            ],\n",
       "            \"selected\": {},\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14,\n",
       "            \"backgroundColor\": \"transparent\",\n",
       "            \"borderColor\": \"#ccc\",\n",
       "            \"borderRadius\": 0,\n",
       "            \"pageButtonItemGap\": 5,\n",
       "            \"pageButtonPosition\": \"end\",\n",
       "            \"pageFormatter\": \"{current}/{total}\",\n",
       "            \"pageIconColor\": \"#2f4554\",\n",
       "            \"pageIconInactiveColor\": \"#aaa\",\n",
       "            \"pageIconSize\": 15,\n",
       "            \"animationDurationUpdate\": 800,\n",
       "            \"selector\": false,\n",
       "            \"selectorPosition\": \"auto\",\n",
       "            \"selectorItemGap\": 7,\n",
       "            \"selectorButtonGap\": 10\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"enterable\": false,\n",
       "        \"confine\": false,\n",
       "        \"appendToBody\": false,\n",
       "        \"transitionDuration\": 0.4,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5,\n",
       "        \"order\": \"seriesAsc\"\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"name\": \"\\u60c5\\u611f\\u7c7b\\u578b\",\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": true,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"animation\": true,\n",
       "            \"animationThreshold\": 2000,\n",
       "            \"animationDuration\": 1000,\n",
       "            \"animationEasing\": \"cubicOut\",\n",
       "            \"animationDelay\": 0,\n",
       "            \"animationDurationUpdate\": 300,\n",
       "            \"animationEasingUpdate\": \"cubicOut\",\n",
       "            \"animationDelayUpdate\": 0,\n",
       "            \"data\": [\n",
       "                \"\\u6b63\\u9762\",\n",
       "                \"\\u8d1f\\u9762\",\n",
       "                \"\\u4e2d\\u6027\"\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"name\": \"\\u6570\\u91cf\",\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": true,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"animation\": true,\n",
       "            \"animationThreshold\": 2000,\n",
       "            \"animationDuration\": 1000,\n",
       "            \"animationEasing\": \"cubicOut\",\n",
       "            \"animationDelay\": 0,\n",
       "            \"animationDurationUpdate\": 300,\n",
       "            \"animationEasingUpdate\": \"cubicOut\",\n",
       "            \"animationDelayUpdate\": 0\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"text\": \"\\u8bc4\\u8bba\\u60c5\\u611f\\u5206\\u5e03\\u67f1\\u72b6\\u56fe\",\n",
       "            \"target\": \"blank\",\n",
       "            \"subtarget\": \"blank\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"textAlign\": \"auto\",\n",
       "            \"textVerticalAlign\": \"auto\",\n",
       "            \"triggerEvent\": false\n",
       "        }\n",
       "    ],\n",
       "    \"visualMap\": {\n",
       "        \"show\": true,\n",
       "        \"type\": \"continuous\",\n",
       "        \"min\": 167,\n",
       "        \"max\": 505,\n",
       "        \"inRange\": {\n",
       "            \"color\": [\n",
       "                \"#D7DA8B\",\n",
       "                \"#E15457\"\n",
       "            ]\n",
       "        },\n",
       "        \"calculable\": true,\n",
       "        \"inverse\": false,\n",
       "        \"splitNumber\": 5,\n",
       "        \"hoverLink\": true,\n",
       "        \"orient\": \"horizontal\",\n",
       "        \"left\": \"center\",\n",
       "        \"padding\": 5,\n",
       "        \"showLabel\": true,\n",
       "        \"itemWidth\": 20,\n",
       "        \"itemHeight\": 140,\n",
       "        \"borderWidth\": 0\n",
       "    }\n",
       "};\n",
       "                chart_843e121b605b487eb7b4dbae98fbc420.setOption(option_843e121b605b487eb7b4dbae98fbc420);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x1b9e2342f10>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# ========== 情感分析 ==========\n",
    "\n",
    "# 5. 情感分析柱状图\n",
    "def analyze_sentiment(text):\n",
    "    try:\n",
    "        s = SnowNLP(str(text))\n",
    "        score = s.sentiments\n",
    "        if score > 0.6:\n",
    "            return '正面'\n",
    "        elif score < 0.4:\n",
    "            return '负面'\n",
    "        else:\n",
    "            return '中性'\n",
    "    except Exception:\n",
    "        return '中性'  # 异常情况默认为中性\n",
    "\n",
    "print(\"正在执行情感分析，这可能需要一些时间...\")\n",
    "df['情感'] = df['评论'].apply(analyze_sentiment)\n",
    "sentiment_counts = df['情感'].value_counts()\n",
    "\n",
    "sentiment_bar = (\n",
    "    Bar()\n",
    "    .add_xaxis(sentiment_counts.index.tolist())\n",
    "    .add_yaxis(\"评论数量\", sentiment_counts.values.tolist())\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title=\"评论情感分布柱状图\"),\n",
    "        xaxis_opts=opts.AxisOpts(name=\"情感类型\"),\n",
    "        yaxis_opts=opts.AxisOpts(name=\"数量\"),\n",
    "        visualmap_opts=opts.VisualMapOpts(\n",
    "            max_=sentiment_counts.max(),\n",
    "            min_=sentiment_counts.min(),\n",
    "            orient=\"horizontal\",\n",
    "            pos_left=\"center\",\n",
    "            range_color=[\"#D7DA8B\", \"#E15457\"]\n",
    "        )\n",
    "    )\n",
    "    .set_series_opts(\n",
    "        itemstyle_opts={\n",
    "            \"normal\": {\n",
    "                \"color\": JsCode(\"\"\"function(params) {\n",
    "                    if (params.value > 150) {\n",
    "                        return '#E15457';\n",
    "                    } else if (params.value > 100) {\n",
    "                        return '#F9A589';\n",
    "                    } else {\n",
    "                        return '#D7DA8B';\n",
    "                    }\n",
    "                }\"\"\")\n",
    "            }\n",
    "        },\n",
    "        label_opts=opts.LabelOpts(position=\"top\")\n",
    "    )\n",
    ")\n",
    "sentiment_bar.render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-24T13:58:33.008026Z",
     "start_time": "2025-04-24T13:58:32.993709Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "可视化分析报告已成功保存到 weibo_analysis.html\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 6. 整合所有图表（情感分析图表最后添加）\n",
    "page = Page(layout=Page.DraggablePageLayout)\n",
    "page.add(\n",
    "    region_line,\n",
    "    gender_pie,\n",
    "    comment_wordcloud,\n",
    "    sentiment_bar  # 情感分析图表放在最后\n",
    ")\n",
    "\n",
    "# 保存为HTML文件\n",
    "try:\n",
    "    output_file = \"weibo_analysis.html\"\n",
    "    page.render(output_file)\n",
    "    print(f\"可视化分析报告已成功保存到 {output_file}\")\n",
    "except Exception as e:\n",
    "    print(f\"保存文件时出错: {str(e)}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
